import {Config} from 'config.js';

class Token {
	constructor()
	{
		this.verifyUrl =Config.resUrl + 'token/verify';
		this.tokenUrl=Config.resUrl + 'token/user';
	}
	verify()
	{
		var token =wx.getStorageSync('token');
    
		if(!token)
		{
			this.getTokenFromServer();
		}else
		{
			this._veirfyFromServer(token);
		}
	}
	_veirfyFromServer(token)
	{
		var that=this;
		wx.request({
			url:that.verifyUrl,
			method:'POST',
			data:{
				token:token
			},
			success:function(res)
			{
				var valid=res.data.isValid;
				if(!valid)
				{
					that.getTokenFromServer();
				}
			}
		})
	}
	getTokenFromServer(callBack)
	{
		var that=this;
		wx.login({
			success:function(res)
			{
				wx.request({
					url:that.tokenUrl,
					method:'POST',
					data:{
						code:res.code
					},
					success:function(res)
					{
            wx.setStorageSync('token', res.data.token);
						callBack&&callBack(res.data.token);
					}
				})
			}
		})
	}
}

export {Token};